//
//  AppDelegate.m
//  SinaBlog_OC
//
//  Created by macs on 15/11/10.
//  Copyright © 2015年 BlackTail. All rights reserved.
//

#import "AppDelegate.h"
#import "XFMainTabViewController.h"
#import "XFWelcomeViewController.h"
#import "XFNewFeatureViewController.h"
#import "XFUserAccountModel.h"
#import "XFComposeViewController.h"
#import "XFPictureSelectController.h"
#import <BugHD/BugHD.h> //导入头文件
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  // Override point for customization after application launch.
  self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];

  //    self.window.rootViewController = [[XFPictureSelectController alloc]
  //    init];

  self.window.rootViewController = [self defaultViewController];

  [self.window makeKeyAndVisible];

  [[NSNotificationCenter defaultCenter]
      addObserver:self
         selector:@selector(selectViewcontroller:)
             name:@"selectViewcontroller"
           object:nil];
  [BugHD handleCrashWithKey:@"8023e8b8cbd3b42344b4d870689e2d80"];
  return YES;
}

- (void)selectViewcontroller:(NSNotification *)notify {

  if (notify.object != nil) {
    self.window.rootViewController = [[XFWelcomeViewController alloc] init];
    return;
  }

  self.window.rootViewController = [[XFMainTabViewController alloc] init];
}

- (UIViewController *)defaultViewController {

  if ([[[XFUserAccountModel alloc] init] isLogin]) {

    return [self isUpdateApp] ? [[XFNewFeatureViewController alloc] init]
                              : [[XFWelcomeViewController alloc] init];
  }

  return [[XFMainTabViewController alloc] init];
}

//判断是否是最新版本
- (BOOL)isUpdateApp {
  //获取当前最新版本号

  double version = [kInfoDict[@"CFBundleShortVersionString"] doubleValue];
  //获取上一个版本号
  NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
  double lastVersion = [def doubleForKey:@"version"];

  //存放版本好
  [def setDouble:version forKey:@"version"];

  return version > lastVersion;
}

- (void)applicationWillResignActive:(UIApplication *)application {
  // Sent when the application is about to move from active to inactive state.
  // This can occur for certain types of temporary interruptions (such as an
  // incoming phone call or SMS message) or when the user quits the application
  // and it begins the transition to the background state.
  // Use this method to pause ongoing tasks, disable timers, and throttle down
  // OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
  // Use this method to release shared resources, save user data, invalidate
  // timers, and store enough application state information to restore your
  // application to its current state in case it is terminated later.
  // If your application supports background execution, this method is called
  // instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
  // Called as part of the transition from the background to the inactive state;
  // here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
  // Restart any tasks that were paused (or not yet started) while the
  // application was inactive. If the application was previously in the
  // background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
  // Called when the application is about to terminate. Save data if
  // appropriate. See also applicationDidEnterBackground:.
}

@end
